--TAO FOREIGN KEY (NHAN VIEN - PHAN CONG)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_NHANVIEN_PHANCONG')
BEGIN
	ALTER TABLE PHANCONG
	DROP CONSTRAINT FK_NHANVIEN_PHANCONG
END

ALTER TABLE PHANCONG
ADD CONSTRAINT FK_NHANVIEN_PHANCONG
FOREIGN KEY (MANV)
REFERENCES NHANVIEN(MANV)
--TAO FOREIGN KEY (NHAN VIEN - KHA NANG)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_KHANANG_NHANVIEN')
BEGIN
	ALTER TABLE KHANANG
	DROP CONSTRAINT FK_KHANANG_NHANVIEN
END

ALTER TABLE KHANANG
ADD CONSTRAINT FK_KHANANG_NHANVIEN
FOREIGN KEY (MANV)
REFERENCES NHANVIEN(MANV)
--TAO FOREIGN KEY (KHACHHANG - DATCHO)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_DATCHO_KHACHHANG')
BEGIN
	ALTER TABLE DATCHO
	DROP CONSTRAINT FK_DATCHO_KHACHHANG
END

ALTER TABLE DATCHO
ADD CONSTRAINT FK_DATCHO_KHACHHANG
FOREIGN KEY (MAKH)
REFERENCES KHACHHANG(MAKH)
--TAO FOREIGN KEY (DATCHO - LICHBAY)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_DATCHO_LICHBAY')
BEGIN
	ALTER TABLE DATCHO
	DROP CONSTRAINT FK_DATCHO_LICHBAY
END

ALTER TABLE DATCHO
ADD CONSTRAINT FK_DATCHO_LICHBAY
FOREIGN KEY (NGAYDI,MACB)
REFERENCES LICHBAY(NGAYDI,MACB)
--TAO FOREIGN KEY (KHANANG - LOAIMB)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_KHANANG_LOAIMB')
BEGIN
	ALTER TABLE KHANANG
	DROP CONSTRAINT FK_KHANANG_LOAIMB
END

ALTER TABLE KHANANG
ADD CONSTRAINT FK_KHANANG_LOAIMB
FOREIGN KEY (MALOAI) 
REFERENCES LOAIMB (MALOAI)

--TAO FOREIGN KEY (LICHBAY - CHUYENBAY)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_LICHBAY_CHUYENBAY')
BEGIN
	ALTER TABLE LICHBAY
	DROP CONSTRAINT FK_LICHBAY_CHUYENBAY
END
GO 
ALTER TABLE LICHBAY 
ADD CONSTRAINT FK_LICHBAY_CHUYENBAY
FOREIGN KEY (MACB) 
REFERENCES CHUYENBAY (MACB)

--TAO FOREIGN KEY (MAYBAY - LOAIMB)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_MAYBAY_LOAIMB')
BEGIN
	ALTER TABLE MAYBAY
	DROP CONSTRAINT FK_MAYBAY_LOAIMB
END
GO
ALTER TABLE MAYBAY 
ADD CONSTRAINT FK_MAYBAY_LOAIMB
FOREIGN KEY (MALOAI) 
REFERENCES LOAIMB (MALOAI)

--TAO FOREIGN KEY (LICHBAY - MAYBAY)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_LICHBAY_MAYBAY')
BEGIN
	ALTER TABLE LICHBAY
	DROP CONSTRAINT FK_LICHBAY_MAYBAY
END
GO
ALTER TABLE LICHBAY 
ADD CONSTRAINT FK_LICHBAY_MAYBAY
FOREIGN KEY (SOHIEU,MALOAI) 
REFERENCES MAYBAY (SOHIEU,MALOAI)

--TAO FOREIGN KEY (PHANCONG - LICHBAY)
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_PHANCONG_LICHBAY')
BEGIN
	ALTER TABLE PHANCONG
	DROP CONSTRAINT FK_PHANCONG_LICHBAY
END
GO
ALTER TABLE PHANCONG 
ADD CONSTRAINT FK_PHANCONG_LICHBAY
FOREIGN KEY (NGAYDI,MACB) 
REFERENCES LICHBAY (NGAYDI,MACB)